package com.example.cdpbookstore.dao.impl;

import com.example.cdpbookstore.dao.OrderDetailDao;
import com.example.cdpbookstore.pojo.entity.OrderDetail;
import com.example.cdpbookstore.tool.C3P0Tool;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class OrderDetailDaoImpl implements OrderDetailDao {
    @Override
    public boolean save(OrderDetail orderDetail) {
        String sql = "insert into tb_order_detail (id, orderId, bookId, num, unitPrice, creationTime, updateTime) values (?, ?, ?, ?, ?, ?, ?)";

        try {
            int update = C3P0Tool.queryRunner.update(sql,
                    orderDetail.getId(),
                    orderDetail.getOrderId(),
                    orderDetail.getBookId(),
                    orderDetail.getNum(),
                    orderDetail.getUnitPrice(),
                    orderDetail.getCreationTime(),
                    orderDetail.getUpdateTime());
            return update > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<OrderDetail> listByOrderId(String orderId) {
        String sql = "select * from tb_order_detail where orderId=? order by creationTime";

        try {
            return C3P0Tool.queryRunner.query(sql, new BeanListHandler<>(OrderDetail.class), orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
